package com.winit.common.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.beans.RoleInfo;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.manager.CommonManager;
import com.winit.common.util.ConfigUtil;
import com.winit.common.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangjian on 16/8/29.
 */
@RestController
@RequestMapping("/common")
public class CommonController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private CommonManager commonManager;

    @RequestMapping("/reloadConfigFile")
    @ResponseBody
    public Map<String, Object> reloadConfigFile(HttpServletRequest request) {
        logger.info("重新加载Config.properties文件");
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            return this.noUserRtn();
        }
        if (!userInfo.userHasRole(Constants.USER_ROLE_ADMIN)) {
            rtnMap.put("code", "1");
            return getRtnMap(rtnMap, userInfo, new JSONObject());
        }
        ConfigUtil.reloadPropertFile();
        rtnMap.put("code", "0");
        return getRtnMap(rtnMap, userInfo, new JSONObject());
    }

    /**
     * 清空工程版本的release目录
     *
     * @param request
     * @return
     */
    @RequestMapping("/cleanReleaseDir")
    @ResponseBody
    public Map<String, Object> cleanReleaseDir(HttpServletRequest request) {
        logger.info("清空工程版本的release目录");
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            return this.noUserRtn();
        }
        if (!userInfo.userHasRole(Constants.USER_ROLE_ADMIN)) {
            rtnMap.put("code", "1");
            return getRtnMap(rtnMap, userInfo, new JSONObject());
        }

        commonManager.cleanReleaseDir();
        rtnMap.put("code", "0");
        return getRtnMap(rtnMap, userInfo, new JSONObject());
    }

    /**
     * 清空不用的分支
     *
     * @param request
     * @return
     */
    @RequestMapping("/cleanProject")
    @ResponseBody
    public Map<String, Object> cleanProject(HttpServletRequest request) {
        logger.info("清空不用的分支");
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            return this.noUserRtn();
        }
        commonManager.cleanProject(request.getParameter("tokenId"));
        rtnMap.put("code", "0");
        return getRtnMap(rtnMap, userInfo, new JSONObject());
    }

    /**
     * 恢复被误删的分支
     *
     * @param request
     * @return
     */
    @RequestMapping("/recoverProject")
    @ResponseBody
    public Map<String, Object> recoverProject(HttpServletRequest request) {
        logger.info("恢复被误删的分支");
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            return this.noUserRtn();
        }
        String projectName = request.getParameter("projectName");
        if (StringUtils.isEmpty(projectName)) {
            rtnMap.put("code", "1");
            rtnMap.put("desc", "参数projectName不能为空");
            return getRtnMap(rtnMap, userInfo, new JSONObject());
        }
        rtnMap.put("code", "0");
        rtnMap.put("desc", commonManager.recoverProject(projectName));
        return getRtnMap(rtnMap, userInfo, new JSONObject());
    }
}
